﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.DataPartLib.XProperty.Editors
{
    /// <summary>
    /// SeriesTypeDefEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SeriesTypeDefEditWindow : Window
    {
        public SeriesTypeDef RetSeriesTypeDef = new SeriesTypeDef();

        private ServiceProxy.DataServiceRef.DataResult _dataResult;
        private SeriesTypeDef _XSeriesTypeDef;
        private static List<string> CanUsedRenderAsList = new List<string>() { "Column", "Line", "StepLine", "StackedColumn","Spline","QuickLine" };
        public SeriesTypeDefEditWindow(SeriesTypeDef XSeriesTypeDef, ServiceProxy.DataServiceRef.DataResult dataResult)
        {
            InitializeComponent();
            _XSeriesTypeDef = XSeriesTypeDef;
            _dataResult = dataResult;

            InitGrid();
            ResetText();
            _tbText.KeyDown += _tbText_KeyDown;
            _tbText.LostFocus += _tbText_LostFocus;
        }

        private void InitGrid()
        {
            _grid.Children.Clear();
            _grid.RowDefinitions.Clear();
            if (_dataResult != null && _dataResult.DataSeries != null && _dataResult.DataSeries.Length > 0)
            {
                int rowIndex = 0;
                _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

                foreach (var dataSeries in _dataResult.DataSeries)
                {
                    _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                    TextBlock tb = new TextBlock();
                    tb.Text = string.Format("ID:{0},名称:{1}", dataSeries.Id, dataSeries.Name);
                    tb.Margin = new Thickness(3, 5, 3, 3);
                    ComboBox cb = new ComboBox();
                    cb.Tag = dataSeries.Id;
                    cb.ItemsSource = GetRenderAsList();
                    cb.SelectionChanged += Cb_SelectionChanged;
                    cb.Margin = new Thickness(3);
                    if (_XSeriesTypeDef != null && _XSeriesTypeDef.ContainsKey(dataSeries.Id))
                    {
                        cb.SelectedItem = Enum.Parse(typeof(Visifire.Charts.RenderAs), _XSeriesTypeDef[dataSeries.Id]);
                        RetSeriesTypeDef[dataSeries.Id] = _XSeriesTypeDef[dataSeries.Id];
                    }
                    else
                    {
                        cb.SelectedItem = Visifire.Charts.RenderAs.Column;
                        RetSeriesTypeDef[dataSeries.Id] = "Column";
                    }

                    Grid.SetColumn(tb, 0);
                    Grid.SetRow(tb, rowIndex);
                    Grid.SetColumn(cb, 1);
                    Grid.SetRow(cb, rowIndex);
                    rowIndex++;

                    _grid.Children.Add(tb);
                    _grid.Children.Add(cb);
                }
            }
        }
        private void ResetGrid()
        {
            _grid.Children.Clear();
            _grid.RowDefinitions.Clear();
            if (_dataResult != null && _dataResult.DataSeries != null && _dataResult.DataSeries.Length > 0)
            {
                int rowIndex = 0;
                _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

                foreach (var dataSeries in _dataResult.DataSeries)
                {
                    _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                    TextBlock tb = new TextBlock();
                    tb.Text = string.Format("ID:{0},名称:{1}", dataSeries.Id, dataSeries.Name);
                    tb.Margin = new Thickness(3, 5, 3, 3);
                    ComboBox cb = new ComboBox();
                    cb.Tag = dataSeries.Id;
                    cb.ItemsSource = GetRenderAsList();
                    cb.SelectionChanged += Cb_SelectionChanged;
                    cb.Margin = new Thickness(3);
                    if (RetSeriesTypeDef != null && RetSeriesTypeDef.ContainsKey(dataSeries.Id))
                    {
                        cb.SelectedItem = Enum.Parse(typeof(Visifire.Charts.RenderAs), RetSeriesTypeDef[dataSeries.Id]);
                    }

                    Grid.SetColumn(tb, 0);
                    Grid.SetRow(tb, rowIndex);
                    Grid.SetColumn(cb, 1);
                    Grid.SetRow(cb, rowIndex);
                    rowIndex++;

                    _grid.Children.Add(tb);
                    _grid.Children.Add(cb);
                }
            }
        }

        private void Cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (RetSeriesTypeDef == null)
            {
                RetSeriesTypeDef = new SeriesTypeDef();
            }
            RetSeriesTypeDef[cb.Tag.ToString()] = cb.SelectedItem.ToString();
            ResetText();
        }

        private void ResetText()
        {
            if (RetSeriesTypeDef != null)
            {
                string textValue = string.Empty;
                foreach (var item in RetSeriesTypeDef)
                {
                    textValue += string.Format("{0}:{1},", item.Key, item.Value);
                }
                _tbText.Text = textValue.TrimEnd(',');
            }
        }

        private void ResetXSeriesTypeDefByText()
        {
            string XSeriesTypeDef = _tbText.Text;
            if (!string.IsNullOrWhiteSpace(XSeriesTypeDef))
            {
                string[] strs = XSeriesTypeDef.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string str in strs)
                {
                    string[] seriesType = str.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (RetSeriesTypeDef.ContainsKey(seriesType[0]))
                    {
                        RetSeriesTypeDef[seriesType[0]] = seriesType[1];
                    }
                }
            }
            ResetGrid();
        }


        private void _tbText_LostFocus(object sender, RoutedEventArgs e)
        {
            ResetXSeriesTypeDefByText();
        }

        private void _tbText_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                ResetXSeriesTypeDefByText();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private List<Visifire.Charts.RenderAs> GetRenderAsList()
        {
            List<Visifire.Charts.RenderAs> list = new List<Visifire.Charts.RenderAs>();
            foreach (Visifire.Charts.RenderAs item in Enum.GetValues(typeof(Visifire.Charts.RenderAs)))
            {
                if (CanUsedRenderAsList.Contains(item.ToString()))
                {
                    list.Add(item);
                }
            }
            return list;
        }
    }
}
